<template>
    <div class="layout-padding">
        <el-form :inline="true" ref="queryRef" :model="queryParams" label-width="120px">
            <el-form-item label="区段：">
                <el-tree-select v-model="queryParams.section" :data="dataSource"
                    :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                    :render-after-expand="false" check-strictly @change="treeChange">
                </el-tree-select>
            </el-form-item>
            <el-form-item label="工区：">
                <el-tree-select v-model="queryParams.workArea" :data="dataSourceArea"
                    :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                    :render-after-expand="false" check-strictly>
                </el-tree-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="list">
            <el-table-column type="index" width="50" label="序号"></el-table-column>
            <el-table-column label="区段" align="center" prop="sectionName" />
            <el-table-column label="工区" align="center" prop="workAreaName" />
            <el-table-column label="检查时间" align="center" prop="checkTime" />
            <el-table-column label="检查方式" align="center" prop="checkWay" />
            <el-table-column label="检查内容" align="center" prop="checkContent" />
            <el-table-column label="人次" align="center" prop="personTime" />
            <el-table-column label="发现问题" align="center" prop="problem" />
            <el-table-column label="采取措施" align="center" prop="measure" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">编辑</el-button> -->
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 详情对话框 -->
        <el-dialog title="防洪设备检查详情" v-model="open" width="780px" append-to-body>
            <el-form ref="configRef" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="区段：" prop="section">
                            <el-tree-select v-model="form.section" :data="dataSource"
                                :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                                :render-after-expand="false" check-strictly @change="treeChange">
                            </el-tree-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="工区：" prop="workArea">
                            <el-tree-select v-model="form.workArea" :data="dataSourceArea"
                                :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                                :render-after-expand="false" check-strictly>
                            </el-tree-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="检查时间：" prop="checkTime">
                            <el-input v-model="form.checkTime" placeholder="请输入参数里程" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="检查方式：" prop="checkWay">
                            <el-input v-model="form.checkWay" placeholder="请输入参数左右侧" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="检查内容：" prop="checkContent">
                            <el-input v-model="form.checkContent" placeholder="请输入参数安装位置" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="人次：" prop="personTime">
                            <el-input v-model="form.personTime" placeholder="请输入参数防护网类型" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="发现问题：" prop="problem">
                    <el-input v-model="form.problem" placeholder="请输入参数防护网类型" />
                </el-form-item>
                <el-form-item label="采取措施：" prop="measure">
                    <el-input v-model="form.measure" placeholder="请输入参数防护网类型" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">保存</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { listEquipmentCheck, getEquipmentCheckn, addEquipmentCheckn, updateEquipmentCheckn, delEquipmentCheckn } from "@/api/system/floodEqu.js";
import { getTreeList, getDeptBy,delCheck } from "@/api/work";
const { proxy } = getCurrentInstance();
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        submited: true,
        sectionName: undefined,
        workAreaName: undefined
    },
    rules: {
        // section: [{ required: true, message: "参数名称不能为空", trigger: "blur" }],
    }
});
const { queryParams, form, rules } = toRefs(data);

const loading = ref(true);
const list = ref([]);
const total = ref(0);
const open = ref(false);
const title = ref("防洪设备检查详情");


//区段数据
const dataSource = ref([])
const dataSourceArea = ref([])
function getTree() {
    getTreeList().then(res => {
        dataSource.value = res.data
    })
}
getTree()

function treeChange(id) {
    queryParams.value.workArea = ''
    form.value.workArea = ''
    getDeptBy({
        parentId: id
    }).then(res => {
        dataSourceArea.value = res.data
    })
}


/** 查询公告列表 */
function getList() {
    loading.value = true;
    listEquipmentCheck(queryParams.value).then(response => {
        list.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 查看按钮操作 */
function handleView(row) {

}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const id = row.id;
    getEquipmentCheckn(id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改防洪设备检查";
    });

}

/** 表单重置 */
function reset() {
    form.value = {
        sectionName: undefined,
        workAreaName: undefined,
        checkTime: undefined,
        checkWay: undefined,
        checkContent: undefined,
        personTime: undefined,
        problem: undefined,
        measure: undefined,
    };
    proxy.resetForm("configRef");
}

/** 删除按钮操作 */
function handleDelete(row) {
    const id = row.id;
    proxy.$modal.confirm('是否确认删除id为"' + id + '"的数据项？').then(function () {
        return delCheck(id);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["configRef"].validate(valid => {
        if (valid) {
            if (form.value.id != undefined) {
                updateEquipmentCheckn(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addEquipmentCheckn(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

getList();

</script>

<style lang="scss" scoped>
.layout-padding {
    padding: 10px;
}
</style>
